package com.activemq;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

@Component  
public class LogProducer implements CommandLineRunner {  
  
    private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class);  
  
    @Autowired  
    private JmsMessagingTemplate jmsMessagingTemplate;  
  
    @Autowired  
    private Queue queue;
    
    @Autowired
    private Topic topic1;
  
    @Override  
    public void run(String... strings) throws Exception {  
//    	send("This is a log message.");  
//        LOGGER.info("Log Message was sent to the Queue named sample.log");  
    }  
  
    public void send(String msg) {  
        //this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg);  
    	//send queue.
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
        //send topic.
        try {
			User user = new User();
			user.setId(1);
			user.setName("罗生沙");
			user.setPassword("123456");
			user.setCreateTime(new Date());
			
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("name", "Luoshengsha");
			
			ActiveMQObjectMessage message = new ActiveMQObjectMessage();
			message.setObject((Serializable)m);
			//message.setObject((Serializable) user);
			this.jmsMessagingTemplate.convertAndSend(this.topic1, message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}